using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace DailyTestConsole
{
    public class TCPHttp
    {

        private Socket s = null;
        //private IPEndPoint ip = new IPEndPoint(IPAddress.Parse("64.233.189.104"), 80);
        private IPEndPoint ip = new IPEndPoint(IPAddress.Parse("222.73.165.103"), 80);

        public TCPHttp()
        {
            s = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        }

        public string GetResponse(string action)
        {
            string req = GetReq(action);

            string strHead = GetHead();

            Encoding encod = null;

            int iCharSetIndex = strHead.IndexOf("charset=");
            if (iCharSetIndex != -1)
            {
                int iLen = strHead.IndexOf("\r\n", iCharSetIndex);
                string strC = strHead.Substring(iCharSetIndex + "charset=".Length, iLen - iCharSetIndex - "charset=".Length);

                encod = Encoding.GetEncoding(strC);
            }
            else
            {
                encod = Encoding.GetEncoding("UTF-8");
            }

            byte[] buff = Encoding.ASCII.GetBytes(req);
            byte[] receive = new byte[8184];

            Socket sock = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            sock.Connect((EndPoint)ip);
            sock.Send(buff, buff.Length, 0);
            int iRec = 0;
            string Res = "";
            try
            {
                do
                {
                    iRec = sock.Receive(receive, receive.Length, 0);
                    Res += encod.GetString(receive, 0, iRec);
                }
                while (iRec > 0);
            }
            catch (System.Exception ex)
            {
                string str = ex.Message;
            }
            sock.Disconnect(false);
            sock.Close();
            return Res;
        }

        private string GetReq(string action)
        {
            string CRLF = "\r\n";
            StringBuilder sbReq = new StringBuilder();
            sbReq.AppendFormat("{0}  /module/menu_left.jsp?cid=1053007&session=6472b5314295 HTTP/1.1", action);
            sbReq.Append(CRLF);
            sbReq.Append("Accept: */*");
            sbReq.Append(CRLF);
            sbReq.Append("Referer: http://warlord1.xiaoi.com/main/index2.jsp?session=6472b5314295");
            sbReq.Append(CRLF);
            sbReq.Append("Accept-Language: en-us");
            sbReq.Append(CRLF);
            sbReq.Append("Accept-Encoding: gzip, deflate");
            sbReq.Append(CRLF);
            sbReq.Append("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; CIBA; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)");
            sbReq.Append(CRLF);
            sbReq.Append("Host: warlord1.xiaoi.com");
            sbReq.Append(CRLF);
            sbReq.Append("Connection:Close");
            sbReq.Append(CRLF);
            sbReq.Append("Cookie: xiaoi_game_username=kingdomgps@gmail.com; igame_encry_user=fe3bdf95a8049fc264121a581a4c4ba1; __utma=251625487.2527728751636605000.1228814707.1230530748.1230623393.3; __utmz=251625487.1230530748.2.2.utmcsr=wan.xiaoi.com|utmccn=(referral)|utmcmd=referral|utmcct=/gucenter/user!login.do; warlord2v3.identify=e34830f213b6cfdb; JSESSIONID=849B3F42F9B4F87A8E15633B6DED83B3");
            sbReq.Append(CRLF);
            sbReq.Append(CRLF);
            return sbReq.ToString();
        }

        private string GetHead()
        {
            string req = GetReq("HEAD");

            byte[] buffReq = Encoding.ASCII.GetBytes(req);
            byte[] buffRecv = new byte[1024];
            s.Connect((EndPoint)ip);
            s.Send(buffReq, buffReq.Length, 0);
            int iRecv = 0;

            string strHead = "";

            try
            {
                do
                {
                    iRecv = s.Receive(buffRecv, buffRecv.Length, 0);
                    strHead += Encoding.ASCII.GetString(buffRecv, 0, iRecv);
                }
                while(iRecv > 0 );
                s.Disconnect(false);
                s.Close();
            }
            catch (Exception)
            { }

            return strHead;
        }
    }
}
